<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
</head>

<body>
  <h3>传感器采集页面</h3>
  <p id="temSensor">温度: 初始状态</p>
  <button id="temBtn">采集温度</button>
  <script>
    let myData = {
      state: 0
    };

    const temSensor = document.querySelector('#temSensor');
    const temBtn = document.getElementById('temBtn');


    let idx = 0;



    function temSuccess(values) {
      console.log("PROXIMITY" + myData.state);
      myData.state = values[0];
      temSensor.innerHTML = "温度: " + idx + ': ' + values[0];

      $.ajax({
        url: 'http://10.6.2.217:8080/serve',
        data: { id: idx, value: values[0] },
        type: 'GET',
        success: function (data) {
          console.log(data);
        },
        error: function (err) {
          console.log(err);
        }
      })
    }


    function temError(error) {
      console.log(error);
      throw error;
    };

    document.addEventListener("deviceready", function () {
      console.log("Device ready!");

      sensors.enableSensor("AMBIENT_TEMPERATURE");

      // setInterval(function () {
      //   sensors.getState(temSuccess, temError);
      //   idx++;
      // }, 1000);

      temBtn.onclick = function () {
        sensors.getState(temSuccess, temError);
        idx++;
      }

    }, false);

  </script>
</body>

</html>